class Solution {
    //不同路径
    public int uniquePaths(int m, int n) {
      //创建dp表
      int[][] dp = new int[m+1][n+1];
      //初始化
      dp[0][1] = 1;
      //填表
      for(int i = 1; i <= m; i++){
        for(int j = 1; j <= n; j++){
            dp[i][j] = dp[i-1][j] + dp[i][j-1];
        }
      }
      //返回
      return dp[m][n];
    }
    //不同路径II
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        //创建dp表
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[][] dp = new int[m+1][n+1];
        //初始化
        dp[0][1] = 1;
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(obstacleGrid[i-1][j-1] == 1) dp[i][j] = 0;
                else dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }
        return dp[m][n];
    }
    //珠宝的最高价值
    public int jewelleryValue(int[][] frame) {
        //创建dp表
        int m = frame.length;
        int n = frame[0].length;
        int[][] dp = new int[m+1][n+1];
        //初始化
        //填表
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]) + frame[i-1][j-1];
            }
        }
        //返回
        return dp[m][n];
    }
    //下降路径最小和
    public int minFallingPathSum(int[][] matrix) {
        int n = matrix.length;
        //创建dp表
        int[][] dp = new int[n+1][n+2];
        //初始化
        for(int i = 1; i <= n; i++){
            dp[i][0] = Integer.MAX_VALUE;
            dp[i][n+1] = Integer.MAX_VALUE;
        }
        //填表
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <=n; j++){
                dp[i][j] = Math.min(Math.min(dp[i-1][j-1], dp[i-1][j]), dp[i-1][j+1]) + matrix[i-1][j-1];
            }
        }
        //返回
        int min = dp[n][1];
        for(int i = 2; i <= n; i++){
            min = Math.min(dp[n][i], min);
        }
        return min;
    }
    //最小路径和
    public static int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        //创建dp表
        int[][] dp = new int[m+1][n+1];
        //初始化
        for(int i = 1; i <= m; i++){
            dp[i][0] = Integer.MAX_VALUE;
        }
        for(int i = 1; i <= n; i++){
            dp[0][i] = Integer.MAX_VALUE;
        }
        dp[0][1] = 0;
        //填表
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i-1][j-1];
            }
        }
        return dp[m][n];
    }
    //地下城游戏
    public int calculateMinimumHP(int[][] dungeon) {
        //创建dp表
        int m = dungeon.length;
        int n = dungeon[0].length;
        int[][] dp = new int[m+1][n+1];
        //初始化
        for(int i = 0; i <= m; i++){
            dp[i][n] = Integer.MAX_VALUE;
        }
        for(int i = 0; i <= n; i++){
            dp[m][i] = Integer.MAX_VALUE;
        }
        dp[m][n-1] = 1;
        //填表
        for(int i = m-1; i >= 0; i--){
            for(int j = n-1; j >= 0; j--){
                dp[i][j] = Math.min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j];
                dp[i][j] = Math.max(1, dp[i][j]);
            }
        }
        return dp[0][0];
    }

    public static void main(String[] args) {
        int[][] grid = {{1,2,3},{4,5,6}};
        minPathSum(grid);
    }
}